package org.jeecg.modules.exProject.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.exProject.entity.ExProject;
import org.jeecg.modules.exProject.entity.ExSection;
import org.jeecg.modules.exProject.service.IExProjectService;
import org.jeecg.modules.exProject.service.IExSectionService;
import org.jeecg.modules.exTenderFileClariModi.service.IExTenderFileClariModiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Api(tags = "项目标段")
@RestController
@RequestMapping("/exProject/exSection")
@Slf4j
public class ExSectionController {
    @Autowired
    private IExSectionService exSectionService;
    @Autowired
    private IExProjectService exProjectService;

    @ApiOperation(value = "项目标段-分页列表查询", notes = "项目标段-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ExSection>> queryPageList(ExSection exSection,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<ExSection> queryWrapper = QueryGenerator.initQueryWrapper(exSection, req.getParameterMap());
        Page<ExSection> page = new Page<>(pageNo, pageSize);
        IPage<ExSection> pageList = exSectionService.pageQuery(page, exSection);
        List<String> projectIdList = pageList.getRecords().stream().map(ExSection::getProjectId).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(projectIdList)){
            List<ExProject> projectList = exProjectService.getByIds(projectIdList);
            pageList.getRecords().forEach((item -> {
                Optional<ExProject> projectOptional = projectList.stream().filter(obj -> obj.getProjectId().equals(item.getProjectId())).findFirst();
                if (projectOptional.isPresent()) {
                    item.setProjectName(projectOptional.get().getProjectName());
                    item.setProjectCode(projectOptional.get().getProjectCode());
                }
            }));
        }
        return Result.OK(pageList);
    }

    @ApiOperation(value = "项目标段-全部数据", notes = "项目标段-全部数据")
    @GetMapping(value = "/listAll")
    public Result<List<ExSection>> listAll(ExSection exSection, HttpServletRequest req) {
        QueryWrapper<ExSection> queryWrapper = QueryGenerator.initQueryWrapper(exSection, req.getParameterMap());
        List<ExSection> sectionList = exSectionService.list(queryWrapper);
        List<String> projectIdList = sectionList.stream().map(ExSection::getProjectId).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(projectIdList)){
            List<ExProject> projectList = exProjectService.getByIds(projectIdList);
            sectionList.forEach((item -> {
                Optional<ExProject> projectOptional = projectList.stream().filter(obj -> obj.getProjectId().equals(item.getProjectId())).findFirst();
                if (projectOptional.isPresent()) {
                    item.setProjectName(projectOptional.get().getProjectName());
                    item.setProjectCode(projectOptional.get().getProjectCode());
                }
            }));
        }
        return Result.OK(sectionList);
    }
}
